<template>
  <div>
    <ul>
      <li v-for="item of messageList" :key="item.id">
        <!-- 1.跳转路由并携带params参数，to的字符串写法 -->
        <!-- <router-link :to="`/home/message/detail/${item.id}/${item.title}`">{{
          item.title
        }}</router-link> -->

        <!-- 2.跳转路由并携带params参数，to的对象写法 -->
        <router-link
          :to="{
            name: 'xiangqing',
            params: {
              id: item.id,
              title: item.title,
            },
          }"
          >{{ item.title }}
        </router-link>
        <button @click="pushShow(item)">push查看</button>
        <button @click="replaceShow(item)">replace查看</button>
      </li>
    </ul>
    <hr />
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: "Message",
  data() {
    return {
      messageList: [
        { id: "001", title: "消息001" },
        { id: "002", title: "消息002" },
        { id: "003", title: "消息003" },
      ],
    };
  },
  methods: {
    pushShow(item) {
      // 以push的方式跳转路由
      this.$router.push({
        name: "xiangqing",
        params: {
          id: item.id,
          title: item.title,
        },
      });
    },
    replaceShow(item) {
      // 以replace的方式跳转路由
      this.$router.replace({
        name: "xiangqing",
        params: { 
          id: item.id,
          title: item.title,
        },
      });
    },
  },
  /* mounted(){
    console.log("Mssage组件挂载了");
  },
  beforeDestroy(){
    console.log("Message组件销毁了");
  } */
};
</script>
